package com.bw.usercenter.ui.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

public class MyEditTextView  extends androidx.appcompat.widget.AppCompatEditText {
    private static final String TAG = "MyEditTextView";
    private String texts;//记录文字
    private Paint paint;
    String reg="^[0-9]{0,}$";//判断是否是数字
    private Boolean isload=false;//是否加载灰色背景


    float startx;
    float endx;
    float starty;
    float endy;


    public MyEditTextView(Context context) {
        super(context);
        init();
    }

    public MyEditTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyEditTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    public void init(){
        paint=new Paint(Paint.ANTI_ALIAS_FLAG);//抗据此
        paint.setColor(Color.GRAY);
        paint.setStrokeWidth(50f);
        paint.setTextSize(50f);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (isload){
            //                      左边      顶部  距离左边长度    高度
            int length = texts.length();
            paint.setColor(Color.GRAY);
            paint.setStrokeWidth(50f);
//            int i = Integer.parseInt(texts);
            if(texts.matches(reg)){
                canvas.drawRoundRect(20f,30,length*50+5f,getMeasuredHeight()-10,20,20,paint);
                paint.setColor(Color.WHITE);
                canvas.drawText(texts,length*15,80,paint);
                paint.setColor(Color.BLACK);
            }else {
                canvas.drawRoundRect(20f,30,length*70+5f,getMeasuredHeight()-10,20,20,paint);
                paint.setColor(Color.WHITE);
                canvas.drawText(texts,length*15,80,paint);
            }
            startx = length*50-50;
            endx = length*50;
            starty = getMeasuredHeight()/4*3;
            endy = 50;
            paint.setStrokeWidth(10f);
            canvas.drawLine(startx,starty,endx,endy,paint);
            canvas.drawLine(startx,endy,endx,starty,paint);
        }
    }
    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        //当目前输入框的文字不为空的时候
        if (!TextUtils.isEmpty(getText().toString().trim())){
            if (focused){//获取到焦点
                setTextColor(Color.BLACK);
                isload=false;
            }else {
                isload=true;
                texts=getText().toString().trim();
//                setText("");
//                setTextColor(Color.WHITE);
                requestLayout();
            }
        }else {
            isload=false;
            if (texts!=null){
                setText(""+texts);

            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        Log.i(TAG, "getHeight();v   "+getMeasuredHeight());
//        Log.i(TAG, "getWidth  "+getMeasuredWidth());
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //点击的时候选中的x轴>=开始的x距离,x<=结束x的距离,并且y轴的距离大于等于y值
        if (event.getX()>=startx && event.getX()<=endx &&  event.getY()>=endy){
            if (texts!=null){
                setText("");
                texts="";
                isload=false;
                requestLayout();
//            }else {
//                isload=true;
//                requestLayout();
            }
//            Toast.makeText(getContext(), "true", Toast.LENGTH_SHORT).show();
        }else {

//            Toast.makeText(getContext(), "false", Toast.LENGTH_SHORT).show();
        }
        return super.onTouchEvent(event);
    }
}
